﻿// jQuery Light Package Form Plugin
// Version 1.0
//
// Written by Larry PAC
// JSC (http://jsc.neo-pac.com/)
// 22 Oct 2009
//
// Visit http://jsc.neo-pac.com/lpform/ for more information
//
// Usage:
//		new $FORM;
// 
// History:
//		1.01 - Update(22 Oct 2009)
//		1.00 - Released (14 Sep 2009)
//
// License:
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2009 JSC 

var $FORM = function(){
	this._pege;
	this._regexp;
	this._pege;
	this._method;
	this._alivepage;
	this._checkform;
	this._previewpage;
	this._idclass;
	this._request;
	this._data;
	this._disable;
	this._setup();
}

$FORM.prototype = {
	_alivepage: 0,
	_checkform: 0,
	_previewpage: function(num){
		var pagearray = this._page.form
		var noconfirmarray = this._noconfirm
		if(this._alivepage < num && pagearray.length >= num){
			if(!(this._validation())){
				window.scrollTo(0, 0);
				return false;
			}
		} else if(this._alivepage > num && pagearray.length-1 > num) {
			this._validation(1);
		}
		
		if(noconfirmarray.length){
			for(i in noconfirmarray)
				$(this._idclass(noconfirmarray[i])).show();
		}			
		if(pagearray.length > num){
			for(page in pagearray){
				page == num ? $(this._idclass(pagearray[page])).show(): $(this._idclass(pagearray[page])).hide();
				if(this._alivepage > num && pagearray.length-1 == num){
					$(this._idclass(pagearray[page])+" :input:disabled").removeAttr("disabled");
					this._disable.attr("disabled", "disabled");
				}
			}
			$(this._idclass(this._page.confirm.head)).hide();
			$(this._idclass(this._page.confirm.foot)).hide();
			$(this._idclass(this._page.finish)).hide();
		} else if(pagearray.length == num){
			$(this._idclass(this._page.confirm.head)).show();
			if(!(num < this._alivepage))
				this._disable = $(":disabled");
			for(page in pagearray){
				$(this._idclass(pagearray[page])).show();
				$(this._idclass(pagearray[page])+" :input:not("+this._idclass(this._button.next)+","+this._idclass(this._button.back)+")").attr("disabled", "disabled");
			}
			$(this._idclass(this._page.finish)).hide();
			var noconfirmarray = this._noconfirm
			if(noconfirmarray.length){
				for(i in noconfirmarray)
					$(this._idclass(noconfirmarray[i])).hide();
			}
			$(this._idclass(this._page.confirm.foot)).show();
		} else if(pagearray.length < num){	
			$(this._idclass(this._page.confirm.head)).hide();
			for(page in pagearray)
				$(this._idclass(pagearray[page])).hide();
			$(this._idclass(this._page.confirm.foot)).hide();
			new this._request();
		}
		this._alivepage = num;
		window.scrollTo(0, 0);
	},
	_setup: function(){
		var form = $FORM.prototype;
		this._previewpage(0);
		$(this._idclass(this._form.id)).submit(function(){return false});
		for(p in this._regexp){
			var Reg = this._regexp[p];
			for(key in Reg)
				$(this._idclass(this._form.id)+" :input[name='"+key+"']").bind("blur",function(){if(form._checkform){form._validation();}}); 
		}
	},
	_idclass: function(str){
		if(/^[\.#]/.test(str))
		 return(str);
		else
		 return("#"+str);
	},
	_request: function(){
		var form = $FORM.prototype;
		if(form._loading)
			$(form._idclass(form._page.form[0])).before('<div id="lpf_loading">'+form._loading+'</div>');
		$.ajax({
			type: (form._form.method||$(form._idclass(form._form.id)).attr("method")).toUpperCase(),
			data: form._data(),
			url: $(form._idclass(form._form.id)).attr("action"),
			dataType: "text",
			cache: false,
			success: function(data, status){
				form._ajax.success(data, status);
				if(form._loading){
					$("#lpf_loading").animate({opacity:0}, 300, "linear");
					setTimeout(function(){$("#lpf_loading").remove();}, 350);
				}
				$(form._idclass(form._page.finish)).show();		

			},
			error: function(XMLHttpRequest, status, errorThrown) {
    form._ajax.error(XMLHttpRequest, status, errorThrown);
				if(form._loading){
					$("#lpf_loading").animate({opacity:0}, 300, "linear");
					setTimeout(function(){$("#loading").remove();}, 350);
				}
			}
		});
	},
	_data: function(){
		var data = {};
		for(page in this._page.form){
			regexphash = this._page.form[page];
			for(i in this._regexp[regexphash]){
				if($(":input:checked[name="+i+"]").size() > 1 || $(":input:selected [name="+i+"]").size() > 1){
					data[i] = [];
					$(":input[name='"+i+"']").each(function(){
						data[i].push( $(this).val() );
					});
				} else if($(":checkbox[name="+i+"]").size() || $(":radio[name="+i+"]").size()) {
					data[i] = ($(":input:checked[name="+i+"]").val()||'');
				} else if($("select[name="+i+"] > option").size()) {
					data[i] = $("select[name="+i+"] > option:selected").val();
				} else if($(":not(:checkbox :radio :select)[name='"+i+"']")) {
					data[i] = $(":input[name='"+i+"']").val();
				}
				data['lpf_'+i] = ($('#lpf_'+i).val()).replace(/^\s*/,'').replace(/\s*$/,'');
			}
		}
		return data;
	},
	_validation: function(flg){
		//return true;
			var p = this._page.form[this._alivepage];
			var Reg = this._regexp[p];
			var data = this._data();
			this._checkform = 0;
			for(key in Reg){
				if(Reg[key].test(data[key]) || flg){
					this._validate.normal();
				} else if(!/^lpf_/.test(key)) {
					this._validate.error();
					this._checkform++;
				}
			}
			if(this._validate.special)
				this._validate.special();			
			if(this._checkform > 0){
				return false;
			}
			return true;
	}
	
}

$(function(){
	var form = $FORM.prototype;
	var confirmhead = 	$("#"+form._page.confirm.head).get();
	$("#"+form._page.confirm.head).remove();
	$("#"+form._page.form[0]).before(confirmhead);
	var nextbutton = form._idclass(form._button.next);
	var backbutton = form._idclass(form._button.back);
	$(nextbutton).click(function(){form._previewpage(eval(form._alivepage+1))});
	$(backbutton).click(function(){form._previewpage(eval(form._alivepage-1))});
	var regexphash;
	for(page in form._page.form){
		regexphash = form._page.form[page];
		for(i in form._regexp[regexphash]){
			var label = $("label[for="+i+"]").hasClass('lpf_label')?
				$("label[for="+i+"][class*=lpf_label]").text():
				($("label[for="+i+"]").html()+"").replace(/<[^>]+>[^<]+<\/[^>]+>|[\r\n]|<\w+\s?\/?>/gmi,'');
			$("#"+form._form.id).append('<input type="hidden" id="lpf_'+i+'" name="lpf_'+i+'" value="'+label+'">');
		}
	}
//	new $FORM;
});
